package com.gitee.sop.layui.adminserver.module.background.admin.rest;

import com.gitee.sop.layui.adminserver.module.background.admin.dto.SysMenuRoleDTO;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenuRole;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysMenuRoleService;
import com.gitee.sop.layui.adminserver.module.base.bo.ZTreeNode;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * com.zch.admin.base.entity.SysRole模型http接口处理
 *
 * @author: admin
 * @date: 2019-11-13
 * @version: 1.0.0
 */
@RestController
@RequestMapping(value = {"/admin/menuRole"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SysMenuRoleRestController {

    @Lazy
    @Resource
    private SysMenuRoleService baseService;

    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(SysMenuRoleDTO query) {
        SysMenuRole sysMenuRole = new SysMenuRole();
        ObjectUtil.switchAllFieldValue(sysMenuRole, query);
        baseService.add(query.getPid(), sysMenuRole);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "id") Integer id) {
        baseService.deleteSelfAndChildrenById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "id") Integer id, SysMenuRoleDTO query) {
        SysMenuRole sysMenuRole = new SysMenuRole();
        ObjectUtil.switchAllFieldValue(sysMenuRole, query);
        baseService.updateById(sysMenuRole.setId(id));
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"})
    public Result searchAll(String appId) {
        return Result.success(baseService.getListForCurUser(appId));
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") Integer id) {
        SysMenuRole SysRole = baseService.getById(id);
        return Result.success(SysRole);
    }


    @RequestMapping(value = {"/search/tree/{userUuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchMenuTreeByUserUuid(@PathVariable String userUuid, String appId) {
        List<ZTreeNode> list = baseService.searchTreeByUserUuid(userUuid, appId);
        return ObjectUtil.toJson(list);
    }
}
